package com.shuzijun.leetcode.editor.cn;

import java.util.HashSet;

public class RemoveDuplicateNodeLcci{
    public static void main(String[] args) {
         Solution solution = new RemoveDuplicateNodeLcci().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeDuplicateNodes(ListNode head) {
        if (head == null) return head;
        HashSet<Integer> set = new HashSet<>();
        ListNode newHead = new ListNode(0);
        ListNode tail = newHead;
        ListNode p = head;
        while (p != null){
            ListNode tmp = p.next;
            if (!set.contains(p.val)){
                set.add(p.val);
                tail.next = p;
                tail = p;
                tail.next = null;
            }
            p = tmp;
        }
        return newHead.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}